﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aga.GestionDeArchivos.Web.Helpers;
using Aga.GestionDeArchivos.Services;
using Aga.GestionDeArchivos.Web.Models.Depositos;
using AutoMapper;
using Aga.GestionDeArchivos.Domain.Entities;

namespace Aga.GestionDeArchivos.Web.Controllers
{
    public class DepositosController : ControllerBase
    {
        public DepositosController()
        {
            ViewBag.Menu = MenuKey.Admin;
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.ViewDepositos)]
        public ActionResult Index(int? index)
        {
            DepositoService service = new DepositoService();

            IndexModel model = new IndexModel
            {
                Depositos = service.GetPage(index ?? 0, PageSize)
            };

            return View(model);
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.CreateDepositos)]
        public ActionResult Create()
        {
            CreateModel model = new CreateModel();

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.CreateDepositos)]
        public ActionResult Create(CreateModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    DepositoService service = new DepositoService();

                    Deposito deposito = new Deposito();

                    Mapper.CreateMap<CreateModel, Deposito>()
                        .ForMember(m => m.Id, a => a.Ignore());

                    deposito = Mapper.Map<CreateModel, Deposito>(model, deposito);

                    service.Save(deposito);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditDepositos)]
        public ActionResult Edit(int id)
        {
            DepositoService service = new DepositoService();

            Deposito deposito = service.GetById(id);

            Mapper.CreateMap<Deposito, EditModel>();

            var model = Mapper.Map<Deposito, EditModel>(deposito);

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditDepositos)]
        public ActionResult Edit(EditModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    DepositoService service = new DepositoService();

                    Deposito deposito = service.GetById(model.Id);

                    Mapper.CreateMap<EditModel, Deposito>()
                        .ForMember(m => m.Id, a => a.Ignore());

                    deposito = Mapper.Map<EditModel, Deposito>(model, deposito);

                    service.Save(deposito);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.DeleteDepositos)]
        public ActionResult Delete(int id)
        {
            DepositoService service = new DepositoService();

            Deposito deposito = service.GetById(id);

            DeleteModel model = new DeleteModel
            {
                Id = deposito.Id,
                Nombre = deposito.Nombre
            };

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.DeleteDepositos)]
        public ActionResult Delete(DeleteModel model)
        {
            try
            {
                DepositoService service = new DepositoService();

                service.Delete(model.Id);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }
    }
}
